package net.sx.minemodule.mvp.fragments;

import android.os.Bundle;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.widget.Button;

import com.alibaba.fastjson.JSON;
import com.homegoods.resource.SpacesItemDecoration;
import com.homegoods.resource.dialog.BaseBottomSheetDialogFragment;
import com.kedll.library.adapter.MultiSelectAdapter;
import com.kedll.library.adapter.MultiTypeAdapter;

import net.sx.minemodule.R;
import net.sx.minemodule.mvp.bean.RefundArryBean;

import java.util.List;

import io.reactivex.Observable;
import io.reactivex.android.schedulers.AndroidSchedulers;

public class RefundDialogFragment extends BaseBottomSheetDialogFragment {
    RecyclerView mList;
    private OnItemClickListener onItemClickListener;
    MultiSelectAdapter<RefundArryBean> selectAdapter;
    Button button;
    private RefundArryBean bean;

    @Override
    protected int layoutResId() {
        return R.layout.mine_item_refund_dialog;
    }

    public void setOnItemClickListener(OnItemClickListener onItemClickListener) {
        this.onItemClickListener = onItemClickListener;
    }

    @Override
    protected void initView(View view) {
        button = findViewById(R.id.refund_dialog_button);
        mList = findViewById(R.id.refund_dialog_list);
        mList.setNestedScrollingEnabled(false);
//        mList.setLayoutManager(MyUtils.getFlexBoxLayoutManager(getContext()));
        mList.setLayoutManager(new LinearLayoutManager(getContext(), LinearLayoutManager.VERTICAL, false));
        mList.setHasFixedSize(false);
//        mList.addItemDecoration(new SpacesItemDecoration(2, 8));
        selectAdapter = new MultiSelectAdapter<>(false);
        mList.setAdapter(selectAdapter);


        selectAdapter.setOnItemClickListener((MultiTypeAdapter.OnItemClickListener<RefundArryBean>) (v, iItem) -> {
            bean = iItem.getData();
            if (iItem.getData().isSelected()) {
                button.setEnabled(true);
            } else {
                button.setEnabled(false);
            }

        });
        button.setOnClickListener(v -> {
            if (onItemClickListener != null) {
                dismiss();
                onItemClickListener.onItemClick(bean);
            }
        });
    }

    @Override
    protected void loadData(Bundle arguments) {
        String data = arguments.getString("data");
        List<RefundArryBean> detailTypeBeans = JSON.parseArray(data, RefundArryBean.class);
//        selectAdapter.addAll(detailTypeBeans);
        Observable.fromIterable(detailTypeBeans)
                .map(speclistBean -> speclistBean.createItem(selectAdapter))
                .toList()
//                .subscribeOn(Schedulers.computation())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(speclistItems -> selectAdapter.setItems(speclistItems));
    }

    public interface OnItemClickListener {
        void onItemClick(RefundArryBean refundArryBean);
    }
}
